package top.fangw.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.common.util.CollectionUtils;
import io.seata.common.util.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import top.fangw.admin.mapper.SysApplicationMapper;
import top.fangw.admin.mapper.SysMenuMapper;
import top.fangw.admin.pojo.dto.SysMenuDto;
import top.fangw.admin.pojo.entity.SysApplication;
import top.fangw.admin.pojo.entity.SysMenu;
import top.fangw.admin.service.ISysMenuService;
import top.fangw.common.api.vo.InitTenantMenu;
import top.fangw.common.core.exception.BizException;
import top.fangw.common.mybatis.annotation.DyDataSource;
import top.fangw.common.mybatis.service.BaseTreeService;

import java.util.List;
import java.util.stream.Stream;

/**
 * <p>
 * 用途：存储菜单信息 服务实现类
 * </p>
 *
 * @author fangw
 * @since 2024-03-02
 */
@Service
@DS("base_info")
@RequiredArgsConstructor
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    private final SysApplicationMapper sysApplicationMapper;

    private final SysMenuMapper sysMenuMapper;

    @Override
    @DyDataSource(tenantId = "#initTenantMenu.tenantId", autoTenantId = false)
    public void initTenantMenu(InitTenantMenu initTenantMenu) {
        Stream<SysMenu> sysMenuStream = initTenantMenu.getInitMenuVoList().stream().map(item -> BeanUtil.copyProperties(item, SysMenu.class));
        sysMenuStream.forEach(this::save);
    }

    @Override
    public List<SysApplication> getApplicationAndMenu() {
        // 查询应用信息
        LambdaQueryWrapper<SysApplication> queryWrapper = new LambdaQueryWrapper<SysApplication>().eq(SysApplication::getShowFlag, "1").orderByAsc(SysApplication::getShowSort);
        BaseTreeService<SysApplicationMapper, SysApplication> treeService = new BaseTreeService<>(sysApplicationMapper);
        List<SysApplication> sysApplications = treeService.treeList(queryWrapper);
        addMenuInfo(sysApplications);
        return sysApplications;
    }

    /**
     * 添加菜单信息
     * @param sysApplications 应用信息
     */
    private void addMenuInfo(List<SysApplication> sysApplications) {
        sysApplications.forEach(item -> {
            if (item.getChildren() == null || item.getChildren().size() == 0) {
                // 查询并添加菜单信息
                LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getApplicationId,item.getId()).orderByAsc(SysMenu::getSort);
                BaseTreeService<SysMenuMapper, SysMenu> treeService = new BaseTreeService<>(sysMenuMapper);
                item.setChildren(treeService.treeList(queryWrapper));
                System.out.println("item = " + treeService.treeList(queryWrapper));
            } else {
                addMenuInfo(item.getChildren());
            }
        });
    }
}
